var directionDisplay;
var directionsService;
var map;

//MOSTLY COPIED FROM WEB
function initialize() 
{
	var urlArg = new ArgumentURL();
	var businessAddress = urlArg.getArgument("businessAddress");
	var clientAddress = urlArg.getArgument("clientAddress");
	
	directionsService = new google.maps.DirectionsService();
	directionsDisplay = new google.maps.DirectionsRenderer();
	
	if (businessAddress != null && clientAddress != null) {
		document.getElementById("start").value = businessAddress;
		document.getElementById("end").value = clientAddress;
		codeAddress("canada", 4);
		calcRoute();
	}
	
	else {
		codeAddress("canada", 4);
	}
}

//MOSTLY COPIED FROM WEB
function codeAddress(codeAddress, codeZoom) 
{
	var geocoder = new google.maps.Geocoder();
	geocoder.geocode( { 'address': codeAddress}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			var myOptions = {
					zoom:7,
					mapTypeId: google.maps.MapTypeId.ROADMAP,
					center: results[0].geometry.location
			};
			
		  	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		  	directionsDisplay.setMap(map);
		} else {
			alert("Geocode was not successful for the following reason: " + status);
		}
	});
}

//MOSTLY COPIED FROM WEB
function calcRoute() 
{
	var start = document.getElementById("start").value;
	var end = document.getElementById("end").value;
	var request = {
		origin:start, 
		destination:end,
		travelMode: google.maps.DirectionsTravelMode.DRIVING
	};
  
	directionsService.route(request, function(response, status) {
		if (status == google.maps.DirectionsStatus.OK) {
			directionsDisplay.setDirections(response);
		}
		
		else {
			alert("coordinates not found");
		}
	});
}

function initializeEmail()
{
	$.getJSON('/GoGoGadget/AuthenticationServlet', initializeEmailCallback);
}

function initializeEmailCallback(data)
{
	var isLoggedIn = data.value;
	if(isLoggedIn)
	{
		getEmail();
	}	else {
		alert("Please login");
		document.location = "/GoGoGadget/StartPage.jsp";
	}
}

function getEmail()
{
	var url = new ArgumentURL();
	var sender = url.getArgument("sender");
	
	var emailcontent = document.getElementById("emailcontent");

	var servlet = '/GoGoGadget/EmailHeaderServlet?number=' + emailcontent.getAttribute("displayNum") + '&start=' + emailcontent.getAttribute("start");
	if (sender != null) {
		servlet += '&sender=' + sender;
	}
	
	$.getJSON(servlet, displayEmail);
}

function displayEmail(data)
{
	var headers = data.headers;
	var num;
	
	var emailcontent = document.getElementById("emailcontent");
	emailcontent.more = data.more;
	
	offset = emailcontent.start*emailcontent.displayNum;
	
	clearEmailContent();
	
	addInboxButtons();
	
	var table = document.createElement("table");
	
	table.setAttribute("id", "emailtable");
	table.setAttribute("width", "100%");
	table.setAttribute("border", "1");
	table.setAttribute("cellpadding", "0");
	table.setAttribute("cellspacing", "0");
	
	var sendHead = document.createElement("th");
	sendHead.appendChild(document.createTextNode("Sender"));
	
	var subHead = document.createElement("th");
	subHead.appendChild(document.createTextNode("Subject"));
	
	var dateHead = document.createElement("th");
	dateHead.appendChild(document.createTextNode("Date"));
	
	table.appendChild(sendHead);
	table.appendChild(subHead);
	table.appendChild(dateHead);

	for (num = 0; num<headers.length; num++) {
		var header = headers[num];
		var row = document.createElement("tr");
		
		row.setAttribute("index", header.index);
		row.onmouseover = highlight;
		row.onmouseout = unhighlight;
		row.onclick = openMail;

		var sender = document.createElement("td");
		sender.setAttribute("id", "sender");
		var senderData = document.createTextNode(header.sender);
		sender.appendChild(senderData);
		
		var subject = document.createElement("td");
		subject.setAttribute("id", "subject");
		var subjectData = document.createTextNode(header.subject);
		subject.appendChild(subjectData);
		
		var date = document.createElement("td");
		date.setAttribute("id", "date");
		var dateData = document.createTextNode(header.date);
		date.appendChild(dateData);
		
		row.appendChild(sender);
		row.appendChild(subject);
		row.appendChild(date);
		table.appendChild(row);
	}
	
	emailcontent.appendChild(table);
}

function addInboxButtons()
{
	var emailcontent = document.getElementById("emailcontent");
	
	if (emailcontent.more == true) {
		var nextPageButton = document.createElement("button");
		nextPageButton.appendChild(document.createTextNode("Next Page"));
		nextPageButton.onclick = nextPage;
		
		emailcontent.appendChild(nextPageButton);
	}
	
	var start = emailcontent.start;
	
	if (start > 0) {
		var previousPageButton = document.createElement("button");
		previousPageButton.appendChild(document.createTextNode("Previous Page"));
		previousPageButton.onclick = previousPage;
		
		emailcontent.appendChild(previousPageButton);
	}
	
	var composeEmailButton = document.createElement("button");
	composeEmailButton.appendChild(document.createTextNode("Compose Email"));
	composeEmailButton.onclick = composeEmail;
	
	emailcontent.appendChild(composeEmailButton);
}

function highlight()
{
	this.style.backgroundColor = '#dcfac9';
}

function unhighlight()
{
	this.style.backgroundColor = 'white';
}

function openMail()
{
	index = this.getAttribute("index");
	$.getJSON('/GoGoGadget/GetEmailContentServlet?index=' + index, displayEmailContent);
}

function clearEmailContent()
{
	var emailContent = document.getElementById("emailcontent");

	if ( emailContent.hasChildNodes() )
	{
	    while ( emailContent.childNodes.length >= 1 )
	    {
	    	emailContent.removeChild( emailContent.firstChild );       
	    } 
	}
}

function displayEmailContent(data)
{
	clearEmailContent();
	
	var inboxButton = document.createElement("button");
	var buttontext = document.createTextNode("Back To Inbox");
	inboxButton.appendChild(buttontext);
	inboxButton.onclick = getEmail;
	
	var emailcontent = document.getElementById("emailcontent");
	
	var div = document.createElement("div");
	div.innerHTML = data.content;
	emailcontent.appendChild(inboxButton);
	emailcontent.appendChild(div);
}

function nextPage()
{
	emailContent = document.getElementById("emailcontent");
	
	var start = emailContent.start;
	emailContent.start = parseFloat(start) + 1;
	getEmail();
}

function previousPage()
{
	var emailContent = document.getElementById("emailcontent");
	
	var start = emailContent.start;
	emailContent.start = parseFloat(start) - 1;
	getEmail();
}

function composeEmail()
{
	clearEmailContent();
	addComposeButtons();
	
	var emailcontent = document.getElementById("emailcontent");
	
	var label = document.createElement("div");
	label.appendChild(document.createTextNode("To: "));
	
	var toInput = document.createElement("input");
	toInput.setAttribute("id", "to");
	label.appendChild(toInput);
	emailcontent.appendChild(label);
	
	var textArea = document.createElement("textarea");
	textArea.setAttribute("id", "email");
	textArea.setAttribute("rows", "10");
	textArea.setAttribute("cols", "120");
	emailcontent.appendChild(textArea);
}

function addComposeButtons()
{
	var emailcontent = document.getElementById("emailcontent");
	
	var inboxButton = document.createElement("button");
	inboxButton.appendChild(document.createTextNode("Back To Inbox"));
	inboxButton.onclick = getEmail;
	emailcontent.appendChild(inboxButton);
	
	var sendButton = document.createElement("button");
	sendButton.appendChild(document.createTextNode("Send Email"));
	sendButton.onclick = sendEmail;
	
	emailcontent.appendChild(sendButton);
}

function sendEmail()
{
	var to = document.getElementById("to");
	var content = document.getElementById("email");
	
	$.getJSON('/GoGoGadget/SendEmailServlet?to=' + to.value + '&content=' + content.value, getEmail);
}

function getBusinesses()
{
	$.getJSON('/GoGoGadget/listAllBusinessesServlet', displayBusinessNames);
}

function displayBusinessNames(data)
{
	var index;
	var businesses = data.businesses;
	var list = document.getElementById("list");
	
	for (index=0; index<businesses.length; index++) {
		var listElement = document.createElement("li");
		var business = businesses[index];
		listElement.appendChild(document.createTextNode(business));
		listElement.setAttribute("business", business);
		
		listElement.onclick = profile;
		listElement.onmouseover = highlight;
		listElement.onmouseout = unhighlight;
		
		list.appendChild(listElement);
	}
}

function profile()
{
	var business = this.getAttribute("business");
	
	$.getJSON('/GoGoGadget/GetBusinessProfileServlet?business=' + business, getProfile);
}

function getProfile(data)
{
	document.location = "BusinessProfile.jsp?" +
						"business=" + data.business +
						"&telephone=" + data.telephone +
						"&cell=" + data.cell +
						"&city=" + data.city +
						"&email=" + data.email +
						"&fax=" + data.fax +
						"&postal=" + data.postal +
						"&province=" + data.province;
						"&address=" + data.address;
						
}

function inputPayrollOnload()
{
	var urlParser = new ArgumentURL();
	var employee = urlParser.getArgument("empID");
	if (employee != null) 
		document.getElementById("empID").value = employee;
}

function initializePayroll()
{
	$.getJSON('/GoGoGadget/AuthenticationServlet', initializePayrollCallback);
}

function initializePayrollCallback(data)
{
	var isLoggedIn = data.value;
	if(isLoggedIn)
	{
		calculatePayrollOnload();
	}	else {
		alert("Please login");
		document.location = "/GoGoGadget/StartPage.jsp";
	}
}

function calculatePayrollOnload()
{
	
	var urlParse = new ArgumentURL();
	alert(urlParse.getArgument("taxDeduct"));
	document.getElementById("Business").appendChild(document.createTextNode(urlParse.getArgument("business")));
	document.getElementById("EmployeeID").appendChild(document.createTextNode(urlParse.getArgument("empID")));
	document.getElementById("FirstName").appendChild(document.createTextNode(urlParse.getArgument("firstName")));
	document.getElementById("LastName").appendChild(document.createTextNode(urlParse.getArgument("lastName")));
	document.getElementById("Wage").appendChild(document.createTextNode(urlParse.getArgument("wage")));
	document.getElementById("Start").appendChild(document.createTextNode(urlParse.getArgument("start")));
	document.getElementById("End").appendChild(document.createTextNode(urlParse.getArgument("end")));
	document.getElementById("PreTax").appendChild(document.createTextNode(urlParse.getArgument("preTax")));
	document.getElementById("PostTax").appendChild(document.createTextNode(urlParse.getArgument("postTax")));
	document.getElementById("TaxDeduct").appendChild(document.createTextNode(urlParse.getArgument("taxDeduct")));
	document.getElementById("Tax").appendChild(document.createTextNode(urlParse.getArgument("tax")));
	document.getElementById("OT").appendChild(document.createTextNode(urlParse.getArgument("ot")));
	//req.getRequestURI() + "?" + req.getQueryString()
	/*var employeeid = "75";
	var business = "business";
	var sYear = "2010";
	var sMonth = "02";
	var sDay = "1";
	var fYear = "2010";
	var fMonth = "04";
	var fDay = "1";
	
	$.getJSON('/GoGoGadget/CalculatePayrollServlet' +
													'?employeeid=' + employeeid +
													'&business=' + business +
													'&sYear=' + sYear +
													'&sMonth=' + sMonth +
													'&sDay=' + sDay +
													'&fYear=' + fYear +
													'&fMonth=' + fMonth +
													'&fDay=' + fDay,
													payrollCallback);*/
													
}

function payrollCallback(data)
{
	document.getElementById("Business").appendChild(document.createTextNode(data.business));
	document.getElementById("EmployeeID").appendChild(document.createTextNode(data.employeeid));
	document.getElementById("FirstName").appendChild(document.createTextNode(data.firstName));
	document.getElementById("LastName").appendChild(document.createTextNode(data.lastName));
	document.getElementById("Wage").appendChild(document.createTextNode(data.wage));
	document.getElementById("Start").appendChild(document.createTextNode(data.start));
	document.getElementById("End").appendChild(document.createTextNode(data.end));
	document.getElementById("PreTax").appendChild(document.createTextNode(data.totalBeforeTax));
	document.getElementById("PostTax").appendChild(document.createTextNode(data.totalAfterTax));
	document.getElementById("TaxDeduct").appendChild(document.createTextNode(data.taxDeduction));
	document.getElementById("Tax").appendChild(document.createTextNode(data.tax));
}

function submitPayroll()
{
	var employeeid = document.getElementById("EmployeeID").innerText;
	var start = document.getElementById("Start").firstChild.nodeValue;
	var end = document.getElementById("End").firstChild.nodeValue;

	$.getJSON('/GoGoGadget/SubmitPayrollServlet' + 
													'?employeeid=' + employeeid +
													'&start=' + start +
													'&end=' + end,
													submitPayrollCallback);
}

function submitPayrollCallback(data)
{
	var updated = data.updated;
	var message;
	
	if (updated) {
		message = "Time Sheets Successfully Updated.";
	}
	else {
		message = "Time Sheets Not Updated, Error Occured.";
	}
	
	alert(message);
}

function cancelPayroll()
{
	
}

function initializeFinance()
{
	$.getJSON('/GoGoGadget/AuthenticationServlet', initializeFinanceCallback);
}

function initializeFinanceCallback(data)
{
	var isLoggedIn = data.value;
	if(isLoggedIn)
	{
		calculateFinance();
	}	else {
		alert("Please login");
		document.location = "/GoGoGadget/StartPage.jsp";
	}
}

function calculateFinance()
{
	var url = '/GoGoGadget/GetFinanceServlet';
	var isfirst = true;
	var start = document.getElementById("startDate").value;
	var end = document.getElementById("endDate").value;
	
	if (start != "") {
		url += '?start=' + start;
		isfirst = false;
	}
	
	if (end != "") {
		if (isfirst)
			url += '?';
		else
			url += '&';
		
		url += 'end=' + end;
	}
	
	$.getJSON(url, calculateFinanceCallback);
}

function calculateFinanceCallback(data)
{
	if (data.failed) {
		alert(data.message);
		return;
	}
	
	var element;
	
	element = document.getElementById("GrossIncome");
	if (element.hasChildNodes())
		element.replaceChild(document.createTextNode(data.gross), element.firstChild);
	else
		element.appendChild(document.createTextNode(data.gross));
	
	element = document.getElementById("Wages");
	if (element.hasChildNodes())
		element.replaceChild(document.createTextNode(data.wages), element.firstChild);
	else
		element.appendChild(document.createTextNode(data.wages));
	
	element = document.getElementById("Owed");
	if (element.hasChildNodes())
		element.replaceChild(document.createTextNode(data.amtOwed), element.firstChild);
	else
		element.appendChild(document.createTextNode(data.wages));
	
	element = document.getElementById("TaxRate");
	if (element.hasChildNodes()) 
		element.replaceChild(document.createTextNode(data.taxRate), element.firstChild);
	else
		element.appendChild(document.createTextNode(data.taxRate));

	element = document.getElementById("PreNetIncome");
	if (element.hasChildNodes())
		element.replaceChild(document.createTextNode(data.netPreTax), element.firstChild);
	else
		element.appendChild(document.createTextNode(data.netPreTax));
	
	element = document.getElementById("PostNetIncome");
	if (element.hasChildNodes())
		element.replaceChild(document.createTextNode(data.netPostTax), element.firstChild);
	else 
		element.appendChild(document.createTextNode(data.netPostTax));
	
	element = document.getElementById("TaxDeduction");
	if (element.hasChildNodes())
		element.replaceChild(document.createTextNode(data.taxDeduction), element.firstChild);
	else
		element.appendChild(document.createTextNode(data.taxDeduction));
	
	element = document.getElementById("Start");
	if (element.hasChildNodes())
		element.replaceChild(document.createTextNode(data.start), element.firstChild);
	else
		element.appendChild(document.createTextNode(data.start));
	
	element = document.getElementById("End");
	if (element.hasChildNodes())
		element.replaceChild(document.createTextNode(data.end), element.firstChild);
	else
		element.appendChild(document.createTextNode(data.end));
}

function initializeJobList()
{
	$.getJSON('/GoGoGadget/AuthenticationServlet', initializeJobListCallback);
}

function initializeJobListCallback(data)
{
	var isLoggedIn = data.value;
	if(isLoggedIn)
	{
		jobListOnload();
	}	else {
		alert("Please login");
		document.location = "/GoGoGadget/StartPage.jsp";
	}
}

function jobListOnload()
{
	document.getElementById("finished").onclick = jobListCheckFunction;
	document.getElementById("unfinished").onclick = jobListCheckFunction;
	
	getJobList();
}

function jobListCheckFunction(event)
{
	var other;
	
	if(this.id == "finished")
		other = document.getElementById("unfinished");
	else
		other = document.getElementById("finished");
	
	if (other.checked)
	{
		this.checked=false;
	}
}

function getJobList()
{
	var url = '/GoGoGadget/GetJobListServlet';
	
	var checked=1;
	var isfirst=true;
	var element;
	
	element = document.getElementById("estStartLow");
	if (element.value != "") 
	{
		isfirst=false;
		url += '?estStartLow=' + element.value;
	}
	
	element = document.getElementById("estStartHigh");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'estStartHigh=' + element.value;
	}
	
	element = document.getElementById("actStartLow");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'actStartLow=' + element.value;
	}
	
	element = document.getElementById("actStartHigh");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'actStartHigh=' + element.value;
	}
	
	element = document.getElementById("estFinishLow");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'estFinishLow=' + element.value;
	}
	
	element = document.getElementById("estFinishHigh");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'estFinishHigh=' + element.value;
	}
	
	element = document.getElementById("actStartLow");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'actStartLow=' + element.value;
	}
	
	element = document.getElementById("actFinishHigh");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'actFinishHigh=' + element.value;
	}
	
	element = document.getElementById("finished");
	if (element.checked) 
	{
		checked=-1;
	}
	
	element = document.getElementById("unfinished");
	if (element.checked) 
	{
		checked=0;
	}
	
	if(isfirst) 
		url += '?checked=' + checked;
	else
		url += '&checked=' + checked;
	
	$.getJSON(url, getJobListCallBack);
}

function getJobListCallBack(data)
{
	var jobs = data.jobs;
	var list = document.getElementById("list");
	var index;
	
	while (list.hasChildNodes()) {
		list.removeChild(list.firstChild);
	}
	
	for (index=0; index<jobs.length; index++) {
		
		var listElement = document.createElement("li");
		var job = jobs[index];
		listElement.appendChild(document.createTextNode("JobID:  " + job.jobID + "    ClientID:  " + job.clientID));
		listElement.setAttribute("jobID", job.jobID);
		
		listElement.onclick = getJobInfo;
		listElement.onmouseover = highlight;
		listElement.onmouseout = unhighlight;
		
		list.appendChild(listElement);
	}
	
}

function getJobInfo()
{
	var id = this.getAttribute("jobID");

	document.location = "JobInfo.jsp?jobID=" + id;
}

function initializeJobInfo()
{
	$.getJSON('/GoGoGadget/AuthenticationServlet', initializeJobInfoCallback);
}

function initializeJobInfoCallback(data)
{
	var isLoggedIn = data.value;
	if(isLoggedIn)
	{
		jobInfoOnload();
	}	else {
		alert("Please login");
		document.location = "/GoGoGadget/StartPage.jsp";
	}
}

function jobInfoOnload()
{
	var urlParser = new ArgumentURL();
	var jobID = urlParser.getArgument("jobID");
	
	$.getJSON('/GoGoGadget/GetJobInfoServlet?jobID=' + jobID, jobInfoOnloadCallback);
}

function jobInfoOnloadCallback(data)
{
	var job = data.job;
	var text = job.jobID;
	var element;
	if(text!=undefined) {
		element = document.getElementById("jobID");
		element.value = text;
		element.setAttribute("trueValue", text);
	}
	
	var text = job.clientID;
	if(text!=undefined) {
		element = document.getElementById("clientID");
		element.value = text;
		element.setAttribute("trueValue", text);
	}
	
	var text = job.estPrice;
	if(text!=undefined) {
		element = document.getElementById("estPrice");
		element.value = text;
		element.setAttribute("trueValue", text);
	}
	
	var text = job.actPrice;
	if(text!=undefined) {
		element = document.getElementById("actPrice");
		element.value = text;
		element.setAttribute("trueValue", text);
	}
	
	var text = job.amtPaid;
	if(text!=undefined) {
		element = document.getElementById("amtPaid");
		element.value = text;
		element.setAttribute("trueValue", text);
	}
	
	var text = job.expStartDate;
	if(text!=undefined) {
		element = document.getElementById("expStartDate");
		element.value = text;
		element.setAttribute("trueValue", text);
	}
	
	var text = job.actStartDate;
	if(text!=undefined) {
		element = document.getElementById("actStartDate");
		element.value = text;
		element.setAttribute("trueValue", text);
	}
	
	var text = job.streetAd;
	if(text!=undefined) {
		element = document.getElementById("street");
		element.value = text;
		element.setAttribute("trueValue", text);
	}
	
	var text = job.province;
	if(text!=null) {
		element = document.getElementById("province");
		element.value = text;
		element.setAttribute("trueValue", text);
	}
	
	var text = job.city;
	if(text!=undefined) {
		element = document.getElementById("city");
		element.value = text;
		element.setAttribute("trueValue", text);
	}
	
	var text = job.actFinishDate;
	if(text!=undefined) {
		element = document.getElementById("actFinishDate");
		element.value = text;
		element.setAttribute("trueValue", text);
	}

	jobInfoListEmployee(data.employees);
}

function jobInfoListEmployee(employees)
{
	var list = document.getElementById("empList");
	var index;

	for (index=0; index<employees.length; index++) {
		
		var listElement = document.createElement("li");
		var employee = employees[index];
		listElement.appendChild(document.createTextNode(employee.firstName + " " + employee.lastName));
		listElement.setAttribute("empID", employee.empID);
		
		listElement.onclick = getEmployeeInfo;
		listElement.onmouseover = highlight;
		listElement.onmouseout = unhighlight;
		
		list.appendChild(listElement);
	}
}

function getEmployeeInfo()
{
	var employeeid = this.getAttribute("empID");
	alert(employeeid);
	document.location = "EmployeeInfo.jsp?employee=" + employeeid;
}

function jobInfoEdit()
{
	document.getElementById("estPrice").disabled = false;
	document.getElementById("actPrice").disabled = false;
	document.getElementById("amtPaid").disabled = false;
	document.getElementById("expStartDate").disabled = false;
	document.getElementById("actStartDate").disabled = false;
	document.getElementById("street").disabled = false;
	document.getElementById("province").disabled = false;
	document.getElementById("city").disabled = false;
	document.getElementById("actFinishDate").disabled = false;
	
	var buttons = document.getElementById("buttons");
	while(buttons.hasChildNodes())
		buttons.removeChild(buttons.firstChild);
	
	var button;
	
	button = document.createElement("button");
	button.appendChild(document.createTextNode("Submit Changes"));
	button.onclick = submitJobEdit;
	buttons.appendChild(button);
	
	button = document.createElement("button");
	button.appendChild(document.createTextNode("Revert Changes"));
	button.onclick = revertJobEdit;
	buttons.appendChild(button);
}

function submitJobEdit()
{
	var url = '/GoGoGadget/EditJobServlet';
	
	var isfirst=true;
	var element;
	
	element = document.getElementById("jobID");
	if (element.value != "") 
	{
		isfirst=false;
		url += '?jobID=' + element.value;
	}
	
	element = document.getElementById("estPrice");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'estPrice=' + element.value;
	}
	
	element = document.getElementById("actPrice");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'actPrice=' + element.value;
	}
	
	element = document.getElementById("amtPaid");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'amtPaid=' + element.value;
	}
	
	element = document.getElementById("expStartDate");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'expStartDate=' + element.value;
	}
	
	element = document.getElementById("actStartDate");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'actStartDate=' + element.value;
	}
	
	element = document.getElementById("expFinishDate");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'expFinishDate=' + element.value;
	}
	
	element = document.getElementById("actFinishDate");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'actFinishDate=' + element.value;
	}
	
	element = document.getElementById("street");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'street=' + element.value;
	}
	
	element = document.getElementById("province");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'province=' + element.value;
	}
	
	element = document.getElementById("city");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'city=' + element.value;
	}
	
	$.getJSON(url, submitJobInfoCallBack);
}

function submitJobInfoCallBack(data)
{
	if(data.failed) {
		alert(data.message);
	}
	reset();
	jobInfoOnloadCallback(data);
}

function reset()
{
	var element;
	
	element = document.getElementById("jobID");
	element.disabled = true;
	element.removeAttribute("trueValue");
	element.value="";
	
	element = document.getElementById("clientID");
	element.disabled = true;
	element.removeAttribute("trueValue");
	element.value="";
	
	element = document.getElementById("estPrice");
	element.disabled = true;
	element.removeAttribute("trueValue");
	element.value="";
	
	element = document.getElementById("actPrice");
	element.disabled = true;
	element.removeAttribute("trueValue");
	element.value="";
	
	element = document.getElementById("amtPaid");
	element.disabled = true;
	element.removeAttribute("trueValue");
	element.value="";
	
	element = document.getElementById("expStartDate");
	element.disabled = true;
	element.removeAttribute("trueValue");
	element.value="";
	
	element = document.getElementById("actStartDate");
	element.disabled = true;
	element.removeAttribute("trueValue");
	element.value="";
	
	element = document.getElementById("street");
	element.disabled = true;
	element.removeAttribute("trueValue");
	element.value="";
	
	element = document.getElementById("province");
	element.disabled = true;
	element.removeAttribute("trueValue");
	element.value="";
	
	element = document.getElementById("city");
	element.disabled = true;
	element.removeAttribute("trueValue");
	element.value="";
	
	element = document.getElementById("actFinishDate");
	element.disabled = true;
	element.removeAttribute("trueValue");
	element.value="";
	
	var buttons = document.getElementById("buttons");
	while(buttons.hasChildNodes())
		buttons.removeChild(buttons.firstChild);
	
	var button = document.createElement("button");
	button.appendChild(document.createTextNode("Edit"));
	button.onclick = jobInfoEdit;
	buttons.appendChild(button);
}

function revertJobEdit()
{
	var element;
	
	element = document.getElementById("jobID");
	element.value = element.getAttribute("trueValue");
	element.disabled = true;
	
	element = document.getElementById("clientID");
	element.value = element.getAttribute("trueValue");
	element.disabled = true;
	
	element = document.getElementById("estPrice");
	element.value = element.getAttribute("trueValue");
	element.disabled = true;
	
	element = document.getElementById("actPrice");
	element.value = element.getAttribute("trueValue");
	element.disabled = true;
	
	element = document.getElementById("amtPaid");
	element.value = element.getAttribute("trueValue");
	element.disabled = true;
	
	element = document.getElementById("expStartDate");
	element.value = element.getAttribute("trueValue");
	element.disabled = true;
	
	element = document.getElementById("actStartDate");
	element.value = element.getAttribute("trueValue");
	element.disabled = true;
	
	element = document.getElementById("street");
	element.value = element.getAttribute("trueValue");
	element.disabled = true;
	
	element = document.getElementById("province");
	element.value = element.getAttribute("trueValue");
	element.disabled = true;
	
	element = document.getElementById("city");
	element.value = element.getAttribute("trueValue");
	element.disabled = true;
	
	element = document.getElementById("actFinishDate");
	element.value = element.getAttribute("trueValue");
	element.disabled = true;
	
	var buttons = document.getElementById("buttons");
	while(buttons.hasChildNodes())
		buttons.removeChild(buttons.firstChild);
	
	var button = document.createElement("button");
	button.appendChild(document.createTextNode("Edit"));
	button.onclick = jobInfoEdit;
	buttons.appendChild(button);
}

function deleteJob()
{
	var jobID = document.getElementById("jobID");
	$.getJSON('/GoGoGadget/DeleteJobServlet?jobID=' + jobID.value, deleteJobCallback);
}

function deleteJobCallback(data)
{
	if (data.failed)
		alert("Could Not Delete Job");
	
	else {
		alert("Job Successfully Removed");
		document.location = "/GoGoGadget/JobList.jsp";
	}
}

function findJobLocation()
{
	var address = "";
	var isfirst = true;
	var street;
	var province;
	
	street = document.getElementById("street").value;
	province = document.getElementById("province").value;
	
	if (street != "") {
		address += street;
		isfirst = false;
	}
	
	if (province != "") {
		if (!isfirst)
			address += ", ";
		address += province;
		isfirst = false;
	}
	
	if (isfirst) 
		alert("There is no address for this job");
	
	
	else
		$.getJSON("/GoGoGadget/ClientToGoogleMapServlet?cAddress=" + address, findJobCallback);
}

function findJobCallback(data)
{
	document.location = "/GoGoGadget/GoogleMapsPage.jsp?clientAddress=" + data.cAddress + "&businessAddress=" + data.address;
}

function initializeTimeSheetList()
{
	$.getJSON('/GoGoGadget/AuthenticationServlet', initializeTimeSheetListCallback);
}

function initializeTimeSheetListCallback(data)
{
	var isLoggedIn = data.value;
	if(isLoggedIn)
	{
		TimeSheetListOnload();
	}	else {
		alert("Please login");
		document.location = "/GoGoGadget/StartPage.jsp";
	}
}

function TimeSheetListOnload()
{
	
	var urlParser = new ArgumentURL();
	
	var employee = urlParser.getArgument("employee");
	var job = urlParser.getArgument("job");
	
	if(employee != null) {
		document.getElementById("employeeSearch").value = employee;
	}
	
	if (job != null) {
		document.getElementById("jobSearch").value = job;
	}
	
	getTimeSheets();
}

function getTimeSheets()
{
	$.getJSON('/GoGoGadget/GetTimeSheetServlet?jobID=' + document.getElementById("jobSearch").value 
			+ '&empID=' + document.getElementById("employeeSearch").value 
			+ '&low=' + document.getElementById("lowSearch").value 
			+ '&high=' + document.getElementById("highSearch").value, getTimeSheetsCallback);
}

function getTimeSheetsCallback(data)
{
	if (data.failed) {
		alert(data.message);
		document.location = "StartPage.jsp";
		return;
	}
	
	var sheets = data.timeSheets;
	
	var list = document.getElementById("list");
	var index;
	
	while (list.hasChildNodes()) {
		list.removeChild(list.firstChild);
	}
	
	list.setAttribute("listRequest", data.listRequest);
	for (index=0; index<sheets.length; index++) {
		var listElement = document.createElement("li");
		var sheet = sheets[index];
		listElement.appendChild(document.createTextNode("Employee Name:  " + sheet.name + "  Job ID:  " + sheet.jobID + "  Date :  " + sheet.date));
		listElement.setAttribute("jobID", sheet.jobID);
		listElement.setAttribute("empID", sheet.empID);
		listElement.setAttribute("date", sheet.date);

		listElement.onclick = getTimeSheetInfo;
		listElement.onmouseover = highlight;
		listElement.onmouseout = unhighlight;
		
		list.appendChild(listElement);
	}
}

function getTimeSheetInfo()
{
	document.location = "TimeSheet.jsp?jobID=" + this.getAttribute("jobID") +
									 "&empID=" + this.getAttribute("empID") +
									 "&date="  + this.getAttribute("date");
}

function initializeTimeSheet()
{
	$.getJSON('/GoGoGadget/AuthenticationServlet', initializeTimeSheetCallback);
}

function initializeTimeSheetCallback(data)
{
	var isLoggedIn = data.value;
	if(isLoggedIn)
	{
		timeSheetOnload();
	}	else {
		alert("Please login");
		document.location = "/GoGoGadget/StartPage.jsp";
	}
}

function timeSheetOnload()
{
	var urlParser = new ArgumentURL();
	var empID = urlParser.getArgument("empID");
	var jobID = urlParser.getArgument("jobID");
	var date = urlParser.getArgument("date");

	$.getJSON('/GoGoGadget/GetTimeSheetInfoServlet?empID=' + empID + '&jobID=' + jobID + '&date=' + date, timeSheetOnloadCallback);
}

function timeSheetOnloadCallback(data)
{
	var sheet = data.timeSheet;
	var employee = data.employee;
	
	var text;
	var element;
	
	text = sheet.jobID;
	if(text!=undefined) {
		element = document.getElementById("jobID");
		element.value = text;
		element.setAttribute("trueValue", text);
	}
	
	text = employee.firstName + " " + employee.lastName;
	if(text!=undefined) {
		element = document.getElementById("empName");
		element.value = text;
		element.setAttribute("trueValue", text);
	}
	
	text = sheet.empID;
	if(text!=undefined) {
		element = document.getElementById("empID");
		element.value = text;
		element.setAttribute("trueValue", text);
	}
	
	text = sheet.date;
	if(text!=undefined) {
		element = document.getElementById("date");
		element.value = text;
		element.setAttribute("trueValue", text);
	}
	
	text = sheet.hours;
	if(text!=undefined) {
		element = document.getElementById("hours");
		element.value = text;
		element.setAttribute("trueValue", text);
	}
	
	text = sheet.payed;
	if(text!=undefined) {
		element = document.getElementById("payed");
		element.value = text;
		element.setAttribute("trueValue", text);
	}
}

function timeSheetInfoEdit()
{
	document.getElementById("hours").disabled = false;
	document.getElementById("payed").disabled = false;
	
	var buttons = document.getElementById("buttons");
	while(buttons.hasChildNodes())
		buttons.removeChild(buttons.firstChild);
	
	var button;
	
	button = document.createElement("button");
	button.appendChild(document.createTextNode("Submit Changes"));
	button.onclick = submitTimeSheetEdit;
	buttons.appendChild(button);
	
	button = document.createElement("button");
	button.appendChild(document.createTextNode("Revert Changes"));
	button.onclick = revertTimeSheetEdit;
	buttons.appendChild(button);
}

function submitTimeSheetEdit()
{
	var url = '/GoGoGadget/EditTimeSheetServlet';
	
	var isfirst=true;
	var element;
	
	element = document.getElementById("jobID");
	if (element.value != "") 
	{
		isfirst=false;
		url += '?jobID=' + element.value;
	}
	
	element = document.getElementById("empID");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'empID=' + element.value;
	}
	
	element = document.getElementById("date");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'date=' + element.value;
	}
	
	element = document.getElementById("hours");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'hours=' + element.value;
	}
	
	element = document.getElementById("payed");
	if (element.value != "") 
	{
		if (isfirst)
			url += '?';
		else
			url += '&';
		isfirst=false;
		url += 'payed=' + element.value;
	}
	
	$.getJSON(url, submitTimeSheetInfoCallBack);
}

function submitTimeSheetInfoCallBack(data)
{
	if(data.failed) {
		alert(data.message);
	}
	resetTimeSheet();

	timeSheetOnloadCallback(data);
	alert("here");
}

function resetTimeSheet()
{
	var element;
	
	element = document.getElementById("hours");
	element.disabled = true;
	element.removeAttribute("trueValue");
	element.value="";
	
	element = document.getElementById("payed");
	element.disabled = true;
	element.removeAttribute("trueValue");
	element.value="";
	
	var buttons = document.getElementById("buttons");
	while(buttons.hasChildNodes())
		buttons.removeChild(buttons.firstChild);
	
	var button = document.createElement("button");
	button.appendChild(document.createTextNode("Edit"));
	button.onclick = timeSheetInfoEdit;
	buttons.appendChild(button);
}

function revertTimeSheetEdit()
{
	var element;
	
	element = document.getElementById("hours");
	element.value = element.getAttribute("trueValue");
	element.disabled = true;
	
	element = document.getElementById("payed");
	element.value = element.getAttribute("trueValue");
	element.disabled = true;
	
	var buttons = document.getElementById("buttons");
	while(buttons.hasChildNodes())
		buttons.removeChild(buttons.firstChild);
	
	var button = document.createElement("button");
	button.appendChild(document.createTextNode("Edit"));
	button.onclick = timeSheetInfoEdit;
	buttons.appendChild(button);
}

function deleteTimeSheet()
{
	var empID = document.getElementById("empID").value;
	var jobID = document.getElementById("jobID").value;
	var date = document.getElementById("date").value;

	$.getJSON('/GoGoGadget/DeleteTimeSheetServlet?empID=' + empID + '&jobID=' + jobID + '&date=' + date, deleteTimeSheetCallback);
}

function deleteTimeSheetCallback(data)
{
	if (data.failed)
		alert("Could Not Delete Time Sheet");
	
	else {
		alert("Time Sheet Successfully Removed");
		document.location = "/GoGoGadget/TimeSheetList.jsp";
	}
}

function initializeCreateTimeSheet()
{
	$.getJSON('/GoGoGadget/AuthenticationServlet', initializeCreateTimeSheetCallback);
}

function initializeCreateTimeSheetCallback(data)
{
	var isLoggedIn = data.value;
	if(isLoggedIn)
	{
		getEmail();
	}	else {
		alert("Please login");
		document.location = "/GoGoGadget/StartPage.jsp";
	}
}

//COPIED FROM THE WEB
function ArgumentURL() 
{
	this.getArgument = _getArg;
	this.setArgument = _setArg;
	this.removeArgument = _removeArg;
	this.toString    = _toString;	//Allows the object to be printed
					//no need to write toString()
	this.arguments   = new Array();

	// Initiation
	var separator = "&";
	var equalsign = "=";
	
	var str = window.location.search.replace(/%20/g, " ");
	var index = str.indexOf("?");
	var sInfo;
	var infoArray = new Array();

	var tmp;
	
	if (index != -1) {
		sInfo = str.substring(index+1,str.length);
		infoArray = sInfo.split(separator);
	}

	for (var i=0; i<infoArray.length; i++) {
		tmp = infoArray[i].split(equalsign);
		if (tmp[0] != "") {
			var t = tmp[0];
			this.arguments[tmp[0]] = new Object();
			this.arguments[tmp[0]].value = tmp[1];
			this.arguments[tmp[0]].name = tmp[0];
		}
	}
	

	
	function _toString() {
		var s = "";
		var once = true;
		for (i in this.arguments) {
			if (once) {
				s += "?";
				once = false;
			}
			s += this.arguments[i].name;
			s += equalsign;
			s += this.arguments[i].value;
			s += separator;
		}
		return s.replace(/ /g, "%20");
	}
	
	function _getArg(name) {
		if(this.arguments[name] == undefined) {
			return null;
		}
		
		if (typeof(this.arguments[name].name) != "string") {
			return null;
		}
		else {
			return this.arguments[name].value;
		}
	}
	
	function _setArg(name,value) {
		this.arguments[name] = new Object();
		this.arguments[name].name = name;
		this.arguments[name].value = value;
	}
	
	function _removeArg(name) {
		this.arguments[name] = null;
	}
	
	return this;
}
